package com.example.springbootdemo.Controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.example.springbootdemo.Service.ICategoryService;
import com.example.springbootdemo.entity.Category;
import com.example.springbootdemo.utils.ApiResult;
import com.example.springbootdemo.utils.PageResult;
import com.example.springbootdemo.utils.PassToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * categoryController
 * 
 * @author ruoyi
 * @date 2025-04-01
 */
@RestController
@RequestMapping("/system/category")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;

    /**
     * 查询category列表
     */
    @PassToken
    @GetMapping("/list")
    public ApiResult list(Category category, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize)
    {
        PageResult pageResult = categoryService.selectCategoryGoodsList(category,pageNum,pageSize);
        return ApiResult.createBySuccess(200,pageResult,"获取成功");
    }

    @PassToken
    @GetMapping("/list1")
    public ApiResult list1(Category category, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize)
    {
        PageResult pageResult = categoryService.selectCategoryList(category,pageNum,pageSize);
        return ApiResult.createBySuccess(200,pageResult,"获取成功");
    }

    /**
     * 导出category列表
     */

//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Category category)
//    {
//        List<Category> list = categoryService.selectCategoryList(category);
//        ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
//        util.exportExcel(response, list, "category数据");
//    }

    /**
     * 获取category详细信息
     */

    @GetMapping(value = "/{cid}")
    public ApiResult getInfo(@PathVariable("cid") Long cid)
    {
        return ApiResult.createBySuccess(200,categoryService.selectCategoryByCid(cid),"获取成功");
    }

    /**
     * 新增category
     */

    @PostMapping
    public ApiResult add(@RequestBody Category category)
    {
        int i = categoryService.insertCategory(category);
        if(i>0){
            return ApiResult.createBySuccess(200,"","添加成功");
        }else{
            return ApiResult.createByFail(500,"添加失败");
        }
    }

    /**
     * 修改category
     */

    @PutMapping
    public ApiResult edit(@RequestBody Category category)
    {
        int i = categoryService.updateCategory(category);
        if(i>0){
            return ApiResult.createBySuccess(200,"","更新成功");
        }else{
            return ApiResult.createByFail(500,"更新失败");
        }
    }

    /**
     * 删除category
     */

	@DeleteMapping("/{cids}")
    public ApiResult remove(@PathVariable Long[] cids)
    {
            int i = categoryService.deleteCategoryByCids(cids);
            if(i>0){
                return ApiResult.createBySuccess(200,"","删除成功");
            }else{
                return ApiResult.createByFail(500,"删除失败");
            }
    }
}
